
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form, $ = layui.jquery,laydate = layui.laydate;

let shareConditionsList = [];
let warehouseList = [];
layer.ready(function () {
    //分摊条件
    let shareConditions = [
        {name: "净重", value: "1"},
        {name: "毛重", value: "2"},
        {name: "体积", value: "3"},
        {name: "数量", value: "4"}
    ];
    shareConditionsList = xmSelectData("#sharingConditions", "sharingConditions", shareConditions);
    form.render('select');

    //获取仓库信息
    $.ajax({
        url:"transfer/getWarehouse",
        success:function(data){
            if(data.status == 200){
                let warehouse = [];
                for(let i in data.data){
                    let tmp = {
                        name:data.data[i].name,
                        value:data.data[i].code
                    }
                    warehouse.push(tmp);
                }
                warehouseList = xmSelectData("#warehouseCode", "warehouseCode", warehouse);
                form.render('select');
            }
        }
    });
});
//表单刷新
form.render();


function xmSelectData(el, name, data) {
    return xmSelect.render({
        el: el,
        name: name,
        tree: {
            show: true,
            showLine: true,
            showFolderIcon: true,
            indent: 20,
            expandedKeys: false
        },
        toolbar:{
            show:true,
            list:['ALL', 'CLEAR']
        },
        height: 'auto',
        data: data,
        on: function ({arr, change, isAdd}) {
            return this
        }
    })
}

let initData = [];
table.render({
    elem: '#offerselect'
    ,data:initData
    ,url:'stock/getRentalFeeInfoByPage'
    ,toolbar: '#offertoolbar'
    ,cols: [[
        {field: 'numbers',width: 50,type:'numbers',title:'序号'}
        ,{field:'warehouseName', title:'分摊仓库',sort:true,width: 120}
        ,{field:'sharingConditions', title:'分摊条件',sort:true,width: 120,templet: function(data){
                switch (data.sharingConditions) {
                    case 1 :
                        return "净重";
                        break;
                    case 2 :
                        return "毛重";
                        break;
                    case 3 :
                        return "体积";
                        break;
                    case 4 :
                        return "数量";
                        break;
                }
            }}
        ,{field:'totalRent', title:'海外仓库租金总额',sort:true,width: 120}
        ,{field:'apportionedDays', title:'分摊天数',sort:true,width: 120}
        ,{field:'dailyApportionmentAmount', title:'每天分摊金额',sort:true,width: 120}
        ,{field:'balance', title:'剩余金额',sort:true,width: 120}
        ,{field:'executionDate', title:'执行日期',sort:true,width: 120}
        ,{fixed: 'right', width:240, toolbar: '#offereditor', title:'操作'}
    ]]
    ,id: 'offertableid'
    ,height: $(document).height() - $('#offerselect').offset().top - 50
    ,page: true
    , limit: 50
    , limits: [50, 100, 200, 300, 500]
});
table.on('tool(offertable)', function(obj){
    let data = obj.data;
    if(obj.event === 'delect'){
        layer.confirm('确定删除吗', confirmInfo(3, 5), function (index) {
            operationData(data, 1, "删除成功");
        });
    }else if(obj.event === 'submits'){
        layer.confirm('确定执行吗', confirmInfo(3, 5), function (index) {
            operationData(data, 2, "执行成功");
        });
    }else if(obj.event === 'complete'){
        layer.confirm('确定终止吗', confirmInfo(3, 5), function (index) {
            operationData(data, 3, "终止成功");
        });
    }
});


function operationData(data, operation, msg){
    let fields = {
        id : data.id,
        operation: operation
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:JSON.stringify(fields),
        url:"stock/rental/operation",
        success:function(res){
            if(res.status == 200){
                layer.msg(msg, {icon: 6});
                table.reload('offertableid', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: data.field
                }, 'data');
                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
}

table.on('toolbar(offertable)', function(obj){
    switch(obj.event){
        case 'news':
            parent.intab("views/jsp/WarehouseManagement/tpl/newRentalFee.jsp","新增海外仓库租金费")
            break;
    };
});

form.on('submit(submitform)', function(data){
    table.reload('offertableid', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

document.getElementById('clear').onclick = function(){
    warehouseList.setValue([ ]);
    shareConditionsList.setValue([ ]);
};

//提示信息渲染
function confirmInfo(icon, anim) {
    let data = {
        icon: icon,
        title: "提示",
        offset: "auto",
        skin: 'layui-layer-molv',
        anim: anim
    }
    return data;
}

//字段是否为空或者零
function isEmpty(obj){
    if(typeof obj == "undefined" || obj == null || obj == "" || obj == 0){
        return true;
    }else{
        return false;
    }
}
